/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package webapps.servlet.backoffice;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import metier.pojo.Client;
import metier.pojo.Compte;
import metier.pojo.Virement;
import metier.session.IClientManager;
import webapps.tools.Message;
import webapps.tools.ParameterModele;
import webapps.tools.Routeur;
import webapps.tools.Tools;

/**
 *
 * @author Adrien
 */
public class AdminVirementController extends AbstractBOServlet {
    
    @EJB
    IClientManager manager;
    
    public static final String[] message = {
        "Le compte a bien été rajoute",
        "Impossible de rajouter le compte!",
        "Vous ne pouvez pas transférer de l'argent sur le même compte",
        "Le transfert a ete effectue avec succes",
        "Le transfert de compte a echoue",
        "Le formulaire de transfert est incomplet",
        "La suppression a bien ete effectuee",
        "Vous ne pouvez pas supprimer cette compte. Vous devez au prealable le vider pour le supprimer.",
        "La suppression a echouee pour une raison inconnue. Veuillez regarder les logs.",
        "Le formulaire de suppression est incomplet"
        
    }; 
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            boolean lack_a_parameter = false;
            
           if (request.getParameter("id") == null || !Tools.estEntier( request.getParameter("id")))
               lack_a_parameter = true;
           
           if (request.getParameter("compte") == null || !Tools.estEntier( request.getParameter("compte")))
               lack_a_parameter = true;
           
           if (lack_a_parameter == true)
           {
               response.sendRedirect("AdminController");
               return;
           }   
           
           int id_client   = Integer.parseInt(request.getParameter ("id"));
           int id_compte   = Integer.parseInt(request.getParameter("compte"));
           Compte lecompte = manager.getCompteWithVirementsAndPossesseur(id_client, id_compte);
           
           if (lecompte == null)
           {
               response.sendRedirect("AdminController");
               return;
           }
           Client leclient = lecompte.getPossesseur();
           
           
           String action = request.getParameter("action");
           String message ="";
           int etat = Tools.PAS_MESSAGE;
           if ( action != null)
           {
               
               Tools.addMessage(message, etat, response);
               response.sendRedirect("AdminVirementController?id=" + leclient.getId());
               return;
           }
           else
           {
               Message unmessage = Tools.getMessage(request, response);
               if (unmessage != null)
               {
                message = unmessage.getContenu();
                etat    = unmessage.getEtat();
               }
           }
           
           request.setAttribute("client", leclient);
           request.setAttribute("compte", lecompte);
           request.setAttribute("virements", lecompte.getVirements());
           
           //Calcul du solde, et total
           Float solde = 0f;
           Float total = 0f;
           for (Virement vi :lecompte.getVirements())
           {
               if (Objects.equals(vi.getDebiteur().getId(), lecompte.getId()))
               {
                   total += vi.getMontant();
               }
               else
               {
                   total -= vi.getMontant();
               }
           }
           solde = lecompte.getMontant() - total;
           request.setAttribute("total", solde + total);
           request.setAttribute("solde", solde);
           
           if (etat != Tools.PAS_MESSAGE)
           {
               request.setAttribute("etat", etat);
               request.setAttribute("message", message);
           }
           
           Routeur r = new Routeur();
           ParameterModele param = new ParameterModele();
           param.setTitreNavigateur("Listes des utilisateurs");
           param.setTitrePage("Compte n° "+ lecompte.getId() + " - " + lecompte.getNom() + " - " + leclient.getNom() + " " + leclient.getPrenom());
           param.setPagecontenu("/backoffice/virements.jsp");
           r.router(request, response,"/backoffice/modele.jsp",param);
        }
    }
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
